package omg.ligong.action;

import java.io.PrintWriter;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import omg.ligong.dao.CourseClassDAO;
import omg.ligong.dao.DAOFactory;
import omg.ligong.dao.impl.CourseClassDAOImpl;
import omg.ligong.domain.CourseClass;
import omg.ligong.util.StrFunc;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONArray;
import org.json.JSONObject;

public class CourseClassMgrAction extends Action {
	private final static String CMD_LIST_ALL = "listall";// 列出所有类型
	private final static String CMD_ADD = "add";
	private final static String CMD_DEL = "del";
	private final static String CMD_EDIT="modify";
	private final static String CMD_COUNT = "count";
	private final static int PAGE_SIZE = 40;// 每页显示40条
	private final static String DATE_FORMATE = "yyyy-MM-dd";

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String cmd = request.getParameter("cmd");
		if (CMD_LIST_ALL.equals(cmd)) {
			return listAll(mapping, request, response);
		} else if (CMD_ADD.equals(cmd)) {
			return addCourseType(mapping, request, response);
		} else if (CMD_DEL.equals(cmd)) {
			return delCourseType(mapping, request, response);
		} else if (CMD_COUNT.equals(cmd)) {
			return countCourseType(mapping, request, response);
		}else if(CMD_EDIT.equals(cmd)){
			return changeCourseTypeName(mapping,request,response);
		}
		return super.execute(mapping, form, request, response);
	}

	private ActionForward changeCourseTypeName(ActionMapping mapping,
			HttpServletRequest request, HttpServletResponse response) throws Exception{
		String src=request.getParameter("src");
		String dest =request.getParameter("dest");
		if (StrFunc.isNull(dest)) {
			JSONObject obj = new JSONObject();
			obj.put("error", "FAIL");
			obj.put("msg", "请输入修改后的课程类型");
			response.setContentType("text/html;charset=gbk");
			PrintWriter out = response.getWriter();
			try {
				out.print(obj.toString());
				out.flush();
			} finally {
				out.close();
			}
		} else {
			JSONObject obj = new JSONObject();
			src=java.net.URLDecoder.decode(src,"UTF-8");
			dest = java.net.URLDecoder.decode(dest, "UTF-8");
			CourseClassDAO dao = DAOFactory.getInstance()
					.getCourseClassManager();
			response.setContentType("text/html;charset=utf-8");
			PrintWriter out = response.getWriter();
			try {
				dao.modifyCourseClass(src, dest);
				out.print(obj.toString());
			} catch (Exception e) {
				obj.put("error", "FAIL");
				obj.put("msg", e.getMessage());
				out.print(obj.toString());
			} finally {
				out.close();
			}
		}
		return null;
	}

	private ActionForward countCourseType(ActionMapping mapping,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CourseClassDAO dao = DAOFactory.getInstance().getCourseClassManager();
		int count = dao.countCourseClass();
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		try {
			out.print(count);
			out.flush();
		} finally {
			out.close();
		}
		return null;
	}

	private ActionForward delCourseType(ActionMapping mapping,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String courseType = request.getParameter("courseclass");
		if (courseType == null || courseType.equals("")) {
			JSONObject obj = new JSONObject();
			obj.put("error", "FAIL");
			obj.put("msg", "请选择要删除的课程类型");
			response.setContentType("text/html;charset=utf-8");
			PrintWriter out = response.getWriter();
			try {
				out.print(obj.toString());
				out.flush();
			} finally {
				out.close();
			}
		} else {
			JSONObject obj = new JSONObject();
			courseType = java.net.URLDecoder.decode(courseType, "UTF-8");
			CourseClassDAO dao = DAOFactory.getInstance()
					.getCourseClassManager();
			response.setContentType("text/html;charset=utf-8");
			PrintWriter out = response.getWriter();
			try {
				dao.delCourseClass(courseType);
				out.print(obj.toString());
			} catch (Exception e) {
				obj.put("error", "FAIL");
				obj.put("msg", e.getMessage());
				out.print(obj.toString());
			} finally {
				out.close();
			}
		}
		return null;
	}

	private ActionForward addCourseType(ActionMapping mapping,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String courseType = request.getParameter("courseclass");
		if (courseType == null || courseType.equals("")) {
			JSONObject obj = new JSONObject();
			obj.put("error", "FAIL");
			obj.put("msg", "请输入增加的课程类型");
			response.setContentType("text/html;charset=gbk");
			PrintWriter out = response.getWriter();
			try {
				out.print(obj.toString());
				out.flush();
			} finally {
				out.close();
			}
		} else {
			JSONObject obj = new JSONObject();
			courseType = java.net.URLDecoder.decode(courseType, "UTF-8");
			CourseClassDAO dao = DAOFactory.getInstance()
					.getCourseClassManager();
			response.setContentType("text/html;charset=utf-8");
			PrintWriter out = response.getWriter();
			try {
				dao.addCourseClass(new CourseClass(-1, courseType, new Date(
						System.currentTimeMillis())));
				out.print(obj.toString());
			} catch (Exception e) {
				obj.put("error", "FAIL");
				obj.put("msg", e.getMessage());
				out.print(obj.toString());
			} finally {
				out.close();
			}
		}
		return null;
	}

	private ActionForward listAll(ActionMapping mapping,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		int currentpage = 0;
		String page = request.getParameter("page");
		if (StrFunc.isNull(page)) {
			currentpage = 1;
		} else {
			currentpage = Integer.parseInt(page);
		}
		CourseClassDAO dao = DAOFactory.getInstance().getCourseClassManager();
		List list = dao.listCourseClass((currentpage - 1) * PAGE_SIZE,
				PAGE_SIZE);
		response.setContentType("text/html;charset=utf-8");
		if (list == null || list.isEmpty()) {
			PrintWriter out = response.getWriter();
			try {
				JSONObject obj = new JSONObject();
				JSONArray arrary = new JSONArray();
				obj.put("total", "0");
				obj.put("rows", arrary);
				out.print(obj.toString());
				out.flush();
			} finally {
				out.close();
			}
		} else {
			JSONArray array = new JSONArray();
			for (int i = 0; i < list.size(); i++) {
				CourseClass type = (CourseClass) list.get(i);
				JSONObject obj = new JSONObject();
				obj.put(CourseClassDAOImpl.FIELD_CLASS_NAME, type
						.getClassName());
				obj.put(CourseClassDAOImpl.FIELD_CLASS_CREATE_DATE,
						date2Str(type.getCreateDate()));
				array.put(i, obj);
			}
			PrintWriter out = response.getWriter();
			try {
				JSONObject obj = new JSONObject();
				obj.put("total", 100);
				obj.put("rows", array);
				out.print(obj.toString());
				out.flush();
			} finally {
				out.close();
			}
		}
		return null;
	}

	public static String date2Str(Date date) {
		SimpleDateFormat format = new SimpleDateFormat(DATE_FORMATE);
		return format.format(date);
	}
}
